﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JRFinances.Models;
using JRFinances.ViewModels.Reviews;
using AutoMapper;
using PagedList;
using System.Data.Objects;

namespace JRFinances.Controllers
{
    [Authorize]
    public class ReviewsController : Controller
    {
        private JRFinancesEntities db = new JRFinancesEntities();

        public ActionResult Index(int? page, string Title, string Author, string DateFrom, string DateTo)
        {
            var reviews = db.Reviews.Include("User").OrderByDescending(x => x.Date).AsQueryable(); // pobieranie wszystkich artykulow
            if (!string.IsNullOrEmpty(Title))
            {
                reviews = reviews.Where(n => n.Title.Contains(Title)); // dodanie filtrowania po tytule jesli wystapil taki parametr
            }
            if (!string.IsNullOrEmpty(Author))
            {
                reviews = reviews.Where(n => n.User.Name == Author); // dodanie filtrowania po autorze jesli wystaplil taki parametr
            }
            if (!string.IsNullOrEmpty(DateFrom))
            {
                DateTime dateTimeDate;
                if (DateTime.TryParse(DateFrom, out dateTimeDate))
                {
                    reviews = reviews.Where(n => EntityFunctions.TruncateTime(n.Date) >= dateTimeDate.Date); // dodanie filtrowania po dacie jesli wystapil taki parametr
                }
            }
            if (!string.IsNullOrEmpty(DateTo))
            {
                DateTime dateTimeDate;
                if (DateTime.TryParse(DateTo, out dateTimeDate))
                {
                    reviews = reviews.Where(n => EntityFunctions.TruncateTime(n.Date) <= dateTimeDate.Date); // dodanie filtrowania po dacie jesli wystapil taki parametr
                }
            }

            var reviewsViewModel = new List<IndexReviewViewModel>();

            reviews.ToList().ForEach(n => reviewsViewModel.Add(Mapper.Map<IndexReviewViewModel>(n)));

            var pageNumber = page ?? 1;
            var onePageOfReviews = reviewsViewModel.ToPagedList(pageNumber, 10);

            return View(onePageOfReviews);
        }

        public ActionResult Create()
        {
            var reviewViewModel = new CreateReviewViewModel() { Date = DateTime.Now };
            return View(reviewViewModel);
        }

        [HttpPost]
        public ActionResult Create(CreateReviewViewModel reviewViewModel)
        {
            if (ModelState.IsValid)
            {
                var review = Mapper.Map<Review>(reviewViewModel);
                review.UserId = db.Users.Single(n => n.Name == User.Identity.Name).UserId;
                db.Reviews.AddObject(review);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(reviewViewModel);
        }

        public ViewResult Details(int id)
        {
            var review = db.Reviews.Include("User").Single(n => n.ReviewId == id);
            var reviewViewModel = Mapper.Map<DetailsReviewViewModel>(review);

            return View(reviewViewModel);
        }

        public ActionResult ManageOwned()
        {
            string currentUser = User.Identity.Name;
            var ownedReviews = db.Reviews.Where(n => n.User.Name == currentUser);

            var ownedReviewsViewModel = new List<ManageOwnedReviewsViewModel>();

            ownedReviews.ToList().ForEach(n => ownedReviewsViewModel.Add(Mapper.Map<ManageOwnedReviewsViewModel>(n)));

            return View(ownedReviewsViewModel);
        }

        public ActionResult Delete(int id)
        {
            string currentUser = User.Identity.Name;

            var review = db.Reviews.Single(n => n.User.Name == currentUser && n.ReviewId == id);
            db.DeleteObject(review);
            db.SaveChanges();

            return RedirectToAction("ManageOwned");
        }
    }
}
